package ch.ffhs.activities;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

/**
 * class AlarmNotification. Implements notification-handling.
 * 
 * @author mf
 */
public class AlarmNotification {

	public static final int ALARM_NOTIFICATION = 001;

	private static Context lastContext = null;
	private static Intent lastIntent = null;

	/**
	 * displays a new notification in status-bar
	 * 
	 * @param context
	 *            an context
	 * @param intent
	 *            intent to display on click on notification
	 * @param title
	 *            title of notification
	 * @param text
	 *            text of notification
	 * @param notificationId
	 *            notification-identification for update and clear
	 */
	public static void displayNotification(Context context, Intent intent,
			String title, String text, Integer notificationId) {
		// store context and intent
		lastContext = context;
		lastIntent = intent;

		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		// display a notification
		Notification.Builder mBuilder = new Notification.Builder(context)
				.setSmallIcon(R.drawable.ic_launcher)
				.setContentTitle(title)
				.setContentText(text)
				.setAutoCancel(true)
				.setDefaults(
						Notification.DEFAULT_VIBRATE
								| Notification.DEFAULT_LIGHTS);

		PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

		mBuilder.setContentIntent(resultPendingIntent);

		NotificationManager mNotifyMgr = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		// display notification
		mNotifyMgr.notify(notificationId, mBuilder.build());
	}

	/**
	 * updates the title and text of notification identified by notification-id
	 * 
	 * @param title
	 *            new title
	 * @param text
	 *            new text
	 * @param notificationId
	 *            id of existing notification
	 */
	public static void updateNotification(String title, String text,
			Integer notificationId) {
		if ((lastContext != null) && (lastIntent != null)) {
			// update a notification
			Notification.Builder mBuilder = new Notification.Builder(
					lastContext).setSmallIcon(R.drawable.ic_launcher)
					.setContentTitle(title).setContentText(text)
					.setAutoCancel(true);

			PendingIntent resultPendingIntent = PendingIntent.getActivity(
					lastContext, 0, lastIntent,
					PendingIntent.FLAG_UPDATE_CURRENT);

			mBuilder.setContentIntent(resultPendingIntent);

			NotificationManager mNotifyMgr = (NotificationManager) lastContext
					.getSystemService(Context.NOTIFICATION_SERVICE);
			// display notification
			mNotifyMgr.notify(notificationId, mBuilder.build());
		}
	}

	/**
	 * clears the notification identified by id
	 * 
	 * @param notificationId
	 *            id of existing notification
	 */
	public static void clearNotification(Integer notificationId) {
		if ((lastContext != null) && (lastIntent != null)) {
			// cancel notification
			NotificationManager mNotifyMgr = (NotificationManager) lastContext
					.getSystemService(Context.NOTIFICATION_SERVICE);
			mNotifyMgr.cancel(ALARM_NOTIFICATION);
		}
	}
}
